<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\YjNews;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Controllers\AdminController;
use Illuminate\Support\Facades\Auth;

class YjNewsController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
       protected $title = "新闻";
     
   
    protected function grid()
    {
        return Grid::make(new YjNews(), function (Grid $grid) {
            $grid->id->sortable();
            $grid->title;
            $grid->time;
            $grid->content->limit(20);
            $grid->writer;
            $grid->created_at;
            $grid->updated_at->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');

            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new YjNews(), function (Show $show) {
            $show->id;
            $show->title;
            $show->time;
            $show->content->unescape();
            $show->writer;
            $show->created_at;
            $show->updated_at;
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new YjNews(), function (Form $form) {
            $form->text('title')->required();
            $admin_message = Auth::guard('admin')->user();//获取当前管理员名称
            $form->datetime("time")->required() ;

            $form->text('writer')->default($admin_message['name'])->customFormat(function  ($value){
                $admin_message = Auth::guard('admin')->user();//获取当前管理员名称
                if ($value === null) {
                    return $admin_message['name'];
                }
                return $value;
            });

            $form->editor('content')->required();
        });
    }
}
